package com.emotionsns;

import java.util.ArrayList;
import java.util.Vector;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.view.Display;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.WindowManager;

public class BallGroup extends ViewGroup {
	Context context;
	Vector<Ball> balls;
	boolean isTouchEventFromBall;
	int[] windowsize;
	Ball touchedBall;

	public BallGroup(Context context) {
		super(context);
		this.context = context;
		this.isTouchEventFromBall = false;
		balls = new Vector<Ball>();
		windowsize = new int[2];
	}

	public void createBall(IEmotion emotion) {
		Ball ball = new Ball(this.context, this);
		ball.emotion = emotion;
		ball.setRunning(true);
		Thread thread = new Thread(ball);
		thread.setDaemon(true);
		thread.start();
		balls.add(ball);
	}

	public void clearBall() {
		balls.clear();
	}

	public int[] windowsize() {
		if (windowsize[0] == 0) {
			Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
			windowsize[0] = display.getWidth();
			windowsize[1] = display.getHeight();
		}
		return windowsize;
	}

	@Override
	protected void dispatchDraw(Canvas canvas) {
		canvas.drawColor(Color.BLACK);
		for (Ball ball : balls) {
			ball.draw(canvas);
		}
		invalidate();
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent e) {
		for (Ball ball : balls) {
			if (!isTouchEventFromBall) {
				ball.dispatchTouchEvent(e);
			}
		}
		if (touchedBall != null) {
			balls.remove(touchedBall);
			touchedBall = null;
		}
		isTouchEventFromBall = false;
		return true;
	}

	@Override
	protected void onLayout(boolean changed, int left, int top, int right, int bottom) {

	}

	@Override
	protected LayoutParams generateDefaultLayoutParams() {
		return new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
	}

	public void Pause() {
		for (Ball ball : balls) {
			ball.setRunning(false);
		}
	}

	public void Resume() {
		for (Ball ball : balls) {
			ball.setRunning(true);
			Thread thread = new Thread(ball);
			thread.setDaemon(true);
			thread.start();
		}
	}

	public void setEmotions(ArrayList<IEmotion> emotions) {
		this.emotions = emotions;
		clearBall();
		createBallThread.interrupt();
		createBallThread.setDaemon(true);
		createBallThread.start();
	}

	private Handler createHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			createBall((IEmotion) msg.obj);
		}
	};

	private ArrayList<IEmotion> emotions;
	private Thread createBallThread = new Thread() {
		@SuppressWarnings("static-access")
		public void run() {
			int count = 0;
			for (IEmotion emotion : emotions) {
				try {
					count++;
					Message msg = new Message();
					msg.obj = emotion;
					createHandler.sendMessage(msg);
					if (count % 10 == 0) {
						createBallThread.sleep(500);
					}
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		};
	};;

}
